/*
 * Copyright 2007-2009 Taglab Limited.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.taglab.chic.taglib;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.TagSupport;
import java.io.PrintWriter;

import com.taglab.chic.taglib.EscapingXmlWriter;

/**
 * Safely dumps an stacktrace to a page.
 *
 * @author Markus Kobler
 */
public class PrintExceptionTag extends TagSupport {

  private final static Logger logger = LoggerFactory.getLogger(PrintExceptionTag.class);

  @Override
  public int doStartTag() throws JspException {

    Exception error = pageContext.getException();

    if( error != null ) {

      error.printStackTrace(new PrintWriter( new EscapingXmlWriter(  pageContext.getOut() ) ));

    } else {

      logger.warn("No exception found in pageContext");
      
    }

    return Tag.SKIP_BODY;
  }

}
